package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.system.domain.dto.DictDataSearchDTO;
import com.hxzy.system.service.ISysDictDataService;
import com.hxzy.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 64689
 */
@Api(tags = "字典查询模型")
@RestController
@RequestMapping(value = "api")
public class DictDataController  extends BaseController {



    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;



    @GetMapping(value = "/system/dict/data/search")
        public R list(DictDataSearchDTO dictData)
    {
        IPage<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return super.pageVO(list);
    }

    /**
     * 导出数据
     * @param dictData
     * @return
     */
//    @GetMapping("/export")
//    public AjaxResult export(SysDictData dictData)
//    {
//        List<SysDictData> list = (List<SysDictData>) dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        return util.exportExcel(list, "字典数据");
//    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/system/dict/data/type/{value}")
    public R dictType(@PathVariable(value = "value") String dictType)
    {
        List<SysDictData> list = this.dictDataService.selectDictDataByType(dictType);
        if (StringUtils.isNull(list))
        {
            list = new ArrayList<SysDictData>();
        }
        return R.okHasData(list);
    }



    /**
     * 查询字典数据详细
     */

    @GetMapping(value = "system/dict/data/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    @PostMapping(value ="/system/data/add" )
    public R add(@Validated @RequestBody SysDictData dict)
    {
        boolean save = this.dictDataService.save(dict);
        if(save){
            return R.ok();
        }
        return R.okHasData("新增失败");
    }





    @PutMapping(value = "/system/data/update")
    public  R  update(@Validated @RequestBody SysDictData dict){
        SysDictData sysDictData = this.dictDataService.selectDictDataById(dict.getDictCode());
        if(StringUtils.isNull(sysDictData)){
            return R.okHasData("修改失败");
        }

        boolean save = this.dictDataService.updateBy(dict);
        if(!save){
            return R.okHasData("修改失败");
        }
        return R.ok();
    }


    /**
     * 删除字典类型
     */


    @GetMapping("/system/data/delete/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
    }








}
